﻿using System;
using System.ServiceModel;

using SharedGenomics.Core;

namespace SharedGenomics.Workbench.Core
{
    public class AuthenticatedWCFProxyFactory : WCFProxyFactory
    {
        public AuthenticatedWCFProxyFactory(ILoginManager loginManager)
        {
            this.LoginManager = loginManager;
        }

        public override ClientBase<T> GetProxy<T>()
        {
            ExceptionHelper.ThrowIf<InvalidOperationException>(!this.LoginManager.IsLoggedIn, "Cannot create authenticated proxy - no login present");
            ClientBase<T> proxy = base.GetProxy<T>();

            this.LoginManager.LoginCredentials.ApplyToWCFProxy(proxy);
            return proxy;
        }

        private ILoginManager LoginManager { get; set; }
    }
}
